<?php $this->load->view('templates/backend/header'); ?>
<?php $this->load->view('templates/backend/lib'); ?>
<h3><?php echo $this->lang->line("refundTicket_list");?></h3>
<style>
.num_cls{
    width:15%;
}
.btn-success,.btn-danger{
    vertical-align: top;
}
.notice{
    color:green;
}
</style>
<div class="well">
    <?php echo form_open();?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php
                //sdt kh
                echo form_label($this->lang->line('phone').' <span class="required">*</span>', 'phone');
                echo form_input(array('name' => 'phone', 'id' => 'phone', 'value' => (isset($refundTicket->phone))?$refundTicket->phone:'','required'=>'required','class'=>'num_cls'));
                echo form_error('phone');
                if ($this->uri->segment(4) == "edit" || in_array($this->session->userdata['group_id'],array(1,79))){
                    echo form_input(array('type'=>'hidden','name' => 'customerID', 'id' => 'customerID', 'value' => (isset($refundTicket->customerID))?$refundTicket->customerID:''));
                } else {
                    echo ' <span id="findCustomer"></span>';
                }
                
                if ($this->uri->segment(4) == "add"){
                    echo '<div id="foneadd">';//begin id foneadd
                    echo '<div id="fone_addon1" class="fonediv">';
                    echo form_label($this->lang->line('booking_place_code').' / Số vé <span class="required">*</span>', 'booking_place_code');
                    echo form_input(array('name' => 'booking_place_code[]','id' => '1', 'value' => (isset($refundTicket->booking_place_code))?$refundTicket->booking_place_code:'','class' => 'num_cls','required'=>'required','readonly' => 'true'));
                    echo form_error('booking_place_code');
                    echo ' ';
                    echo form_button(array('adata' => 1,'content' => $this->lang->line('add'),'class' => 'btn btn-success'));
                    echo '<span id="simple-msg-booking_place_code1"></span>';
                    echo '</div></div>';//end id foneadd
                } else {
                    
                    echo '<div id="foneadd">';
                    foreach(json_decode($refundTicket->booking_place_code) as $key => $value){
                        $count = $key + 1;
                        echo '<div id="fone_addon'.$count.'" class="fonediv">';
                        if($count == 1){
                            echo form_label($this->lang->line('booking_place_code'), 'booking_place_code');
                        }
                        echo form_input(array('name' => 'booking_place_code[]','id' => $count, 'value' => $value,'class' => 'num_cls','required'=>'required'));
                        echo form_error('booking_place_code');
                        echo ' ';
                        if($count == 1){
                            echo form_button(array('adata' => $count,'content' => $this->lang->line('add'),'class' => 'btn btn-success'));
                        } else {
                            echo '<button name="" type="button" adata="'.$count.'" class="btn btn-danger" onclick="javascript:$(this).parent().remove();">Xóa</button>';
                        }
                        echo '<span id="simple-msg-booking_place_code'.$count.'"></span></div>';
                        echo form_input(array('type'=>'hidden', 'name' => 'booking_place_code_fake[]' , 'value' => $value));

                    }
                    echo '</div>';
                }
                
                //hãng hàng không
                echo form_label($this->lang->line('airline').' <span class="required">*</span>', 'airline');
                echo form_input(array('name' => 'airline', 'id' => 'airline', 'value' => (isset($refundTicket->airline))?$refundTicket->airline:'','required'=>'required','maxlength'=>'2','readonly' => 'true'));
                echo form_error('airline');
                //Giá net
                echo form_label($this->lang->line('gia-net-hang').' <span class="required">*</span>', 'net_fare');
                echo form_input(array('name' => 'net_fare', 'id' => 'net_fare', 'value' => ($refundTicket->net_fare)?$refundTicket->net_fare:'','required'=>'required','readonly' => 'true'));
                echo form_error('net_fare');
                echo form_input(array('type' => 'hidden','name' => 'net_fare_fake', 'id' => 'net_fare_fake', 'value' => $refundTicket->net_fare));
                //Phí hoàn trả
                echo form_label($this->lang->line('phi-hoan-tra').' <span class="required">*</span>', 'phihoantra');
                echo form_input(array('name' => 'phihoantra', 'id' => 'phihoantra', 'value' => ($refundTicket->phihoantra)?$refundTicket->phihoantra:'','readonly' => 'true'));
                echo form_error('phihoantra');
                echo form_input(array('type' => 'hidden','name' => 'phihoantra_fake', 'id' => 'phihoantra_fake', 'value' => ($refundTicket->phihoantra)?$refundTicket->phihoantra:'0'));
                //Giá bán
                echo form_label($this->lang->line('thuc-thu-cty').' <span class="required">*</span>', 'sale_fare');
                echo form_input(array('name' => 'sale_fare', 'id' => 'sale_fare', 'value' => ($refundTicket->sale_fare)?$refundTicket->sale_fare:'','required'=>'required','readonly' => 'true'));
                echo form_error('sale_fare');
                echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake', 'id' => 'sale_fare_fake', 'value' => $refundTicket->sale_fare));
                //thực trả
                echo form_label($this->lang->line('thuc-tra-khach').' <span class="required">*</span>', 'thuctra');
                echo form_input(array('name' => 'thuctra', 'id' => 'thuctra', 'value' => ($refundTicket->thuctra)?$refundTicket->thuctra:'','required'=>'required','readonly' => 'true'));
                echo form_error('thuctra');
                echo form_input(array('type' => 'hidden','name' => 'thuctra_fake', 'id' => 'thuctra_fake', 'value' => $refundTicket->thuctra));
                //Phương thức thanh toán
                echo form_label($this->lang->line('payment_method').' <span class="required">*</span>', 'payment_method');
                echo form_dropdown('payment_method', $this->refundTicket_model->_getListPaymentMethod(), ($refundTicket->payment_method)?$refundTicket->payment_method:'','id="payment_method" disabled="disabled"');
                echo form_error('payment_method');
                echo '<span id="payment_method_form"></span>';
                //checkbox
                if(in_array($this->session->userdata['group_id'],array(1,79))){
                    if($refundTicket->payment_info){
                        $info = $this->refundTicket_model->getInfoTable(array('bankid'=>$refundTicket->payment_info),'book_bank');
                        echo form_label($this->lang->line('info').' <span class="required">*</span>', 'info');
                        echo form_textarea(array('value' => $info[0]->bank_name,'readonly' => 'true','rows'=>5));
                        echo form_error('info');
                    }
                    echo form_checkbox(array('name'=>'checkbox_booker','id'=>'checkbox_booker','value'=>0,'checked'=>FALSE,'style'=>'margin:10px;')).'<label style="display:inline-block" class="label" for="checkbox_booker">'.$this->lang->line('loinhuan_booker').'</label>';
                    echo form_checkbox(array('name'=>'checkbox_khach','id'=>'checkbox_khach','value'=>0,'checked'=>FALSE,'style'=>'margin:10px;')).'<label style="display:inline-block" class="label" for="checkbox_khach">'.$this->lang->line('sodu_khach').'</label>';
                    
                    echo form_input(array('name' => 'loinhuan_booker', 'id' => 'loinhuan_booker', 'value' => 0,'placeholder'=>$this->lang->line('loinhuan_booker')));
                    echo form_error('loinhuan_booker');
                    echo form_input(array('type' => 'hidden','name' => 'loinhuan_booker_fake', 'id' => 'loinhuan_booker_fake', 'value' => 0));
                    
                    echo form_input(array('name' => 'sodu_khach', 'id' => 'sodu_khach', 'value' => 0,'placeholder'=>$this->lang->line('sodu_khach')));
                    echo form_error('sodu_khach');
                    echo form_input(array('type' => 'hidden','name' => 'sodu_khach_fake', 'id' => 'sodu_khach_fake', 'value' => 0));
                }
                //note
                echo form_label($this->lang->line('note').' '.$this->lang->line('booker'), 'note');
                echo form_textarea(array('id'=>'note','name'=>'note','value' => (isset($refundTicket->note))?$refundTicket->note:'','readonly' => 'true','rows'=>5));
                echo form_error('note');
                if(in_array($this->session->userdata['group_id'],array(1,79))){
                    echo form_label($this->lang->line('note').' '.$this->lang->line('payment'), 'note_pay');
                    echo form_textarea(array('id'=>'note_pay','name'=>'note_pay','value' => (isset($refundTicket->note_pay))?$refundTicket->note_pay:'','rows'=>3));
                    echo form_error('note_pay');
                }
                //other
                echo form_input(array('type' => 'hidden','name' => 'id','id' => 'id', 'value' => (isset($refundTicket->id))?$refundTicket->id:''));
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php
                if (in_array($this->session->userdata['group_id'],array(1,79)) || $this->uri->segment(4) == "edit"){echo "<script>$('input').removeAttr('readonly');$('#phone').attr('readonly','true');</script>";}//$('input').removeAttr('readonly');
                if(in_array($this->session->userdata['group_id'],array(1,79))){
                    echo form_button(array('id' => 'accept', 'value' => 'accept', 'name' => 'accept', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')).' ';
                    echo form_button(array('id' => 'cancel','content' => $this->lang->line('close'),'class' => 'btn','onclick'=>'parent.jQuery.fancybox.close()'));
                    echo '<span id="simple-msg-submit"></span>';
                } else {
                    echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')).' ';
                    echo '<a href="'.site_url('auth/refundTicket').'" class="btn btn-danger">'.$this->lang->line('cancel').'</a>';
                }
                ?>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>

<script src="<?php echo base_url().'static/templates/backend/js/autoNumeric.js';?>"></script>
<script>
$(document).ready(function() {
    var segment = "<?php echo $this->uri->segment(4);?>"
    if(segment == "edit"){
        $('#note').removeAttr('readonly');
        $('#payment_method').removeAttr('disabled');
    }
    $("#phone").blur(function() {
                var sodt        = $("#phone").val().trim();
                var test        = /^[0-9]+$/.test(sodt);
                if(test == false){
                    alert("<?php echo $this->lang->line('phone_is_invalid');?>");
                    return false;
                }
                if(sodt.length < 10){
                    alert("Số dt phải có ít nhất 10 số!");return false;
                } else {
                    $("#findCustomer").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
                    setTimeout(function () {
                        $.ajax({
                                url : '/auth/voucher/getContactFromPhone',
                                type: "POST",
                                data : {phone:$("#phone").val()},
                                cache: true,
                                success:function(responseData) 
                                {
                                    if(responseData == 0){
                                        alert("<?php echo $this->lang->line("customerNotFound");?>");
                                        $("#findCustomer").hide();
                                        $("#phone").focus();return false;
                                    } else {
                                        var data    = JSON.parse(responseData);
                                        $("#findCustomer").html('<span class="notice">SĐT hợp lệ - Khách hàng: '+data['name']+'</span><input type="hidden" id="customerID" name="customerID" value="'+data['customerID']+'" />').show();
                                        document.getElementById("phone").setAttribute("readonly", "true");
                                        $('.num_cls').removeAttr('readonly');
                                        $('#airline').removeAttr('readonly');
                                    }
                                },
                                error: function() 
                                {

                                }
                        });
                    }, 1000);
                }
        });
    $("#airline").keyup(function() {
        $(this).val($(this).val().replace(/([a-z])/,function(s){return s.toUpperCase()}));
    });
    $("#booking_place_code").keyup(function() {
        $(this).val($(this).val().replace(/([a-z])/,function(s){return s.toUpperCase()}));
    });
    $('#net_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#net_fare").blur(function() {
        document.getElementById("net_fare_fake").value = UnFormatNumber($('#net_fare').val());
    });
    $('#sale_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#sale_fare").blur(function() {
        document.getElementById("sale_fare_fake").value = UnFormatNumber($('#sale_fare').val());
    });
    $('#phihoantra').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#phihoantra").blur(function() {
        document.getElementById("phihoantra_fake").value = UnFormatNumber($('#phihoantra').val());
    });
    $('#thuctra').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#thuctra").blur(function() {
        document.getElementById("thuctra_fake").value   = UnFormatNumber($('#thuctra').val());
        $('#payment_method').removeAttr('disabled');
    });
    $('form').submit(function() {
            var payment_method = $("#payment_method").val();
            if(payment_method == ""){
                alert("Vui lòng chọn PTTT");return false;
            } else {
                $('button[type="submit"]').attr('disabled','disabled');
            }
    });
    $("#airline").on('blur',function(e) {
            if($("#airline").val().trim() == ""){
                $("#airline").focus();return false;
            }
            $('#net_fare').removeAttr('readonly');
            $('#sale_fare').removeAttr('readonly');
            $('#thuctra').removeAttr('readonly');
            $('#phihoantra').removeAttr('readonly');
    });
    $('#loinhuan_booker').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#loinhuan_booker").hide();
    $('#sodu_khach').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#sodu_khach").hide();
    
    $('#checkbox_booker').click (function(){
        if ( $(this).is(':checked') ) {
          // Do stuff
          document.getElementById("checkbox_booker").value                      = 1;
          document.getElementById("loinhuan_booker").value                      = FormatNumber($("#sale_fare_fake").val());
          document.getElementById("loinhuan_booker_fake").value                 = UnFormatNumber($("#loinhuan_booker").val());
          $("#loinhuan_booker").show();
        } else {
          $("#loinhuan_booker").hide();
          document.getElementById("checkbox_booker").value                      = 0;
          document.getElementById("loinhuan_booker").value                      = 0;
          document.getElementById("loinhuan_booker_fake").value                 = 0;
        }
    });
    
    $('#checkbox_khach').click (function(){
        if ( $(this).is(':checked') ) {
          // Do stuff
          document.getElementById("checkbox_khach").value                       = 1;
          document.getElementById("sodu_khach").value                           = FormatNumber($("#thuctra_fake").val());
          document.getElementById("sodu_khach_fake").value                      = UnFormatNumber($("#sodu_khach").val());
          $("#sodu_khach").show();
        } else {
          $("#sodu_khach").hide();
          document.getElementById("checkbox_khach").value                       = 0;
          document.getElementById("sodu_khach").value                           = 0;
          document.getElementById("sodu_khach_fake").value                      = 0;
        }
    });   
    
    $("#accept").on('click',function(e) {
            $("#simple-msg-submit").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
            setTimeout(function (e) {
                $.ajax({
                        url : '/auth/refundTicket/accept',
                        type: "POST",
                        data : { note_pay:$("#note_pay").val(),sodu_khach_fake: $("#sodu_khach_fake").val(),loinhuan_booker_fake: $("#loinhuan_booker_fake").val(), checkbox_booker: $("#checkbox_booker").val(), checkbox_khach: $("#checkbox_khach").val(),id: $("#id").val()},
                        cache: false,
                        beforeSend: function(){

                        },
                        success:function(responseData) 
                        {
                            if(responseData == 1){
                                window.top.location.href="/auth/refundTicket/";
                            }
                        },
                        error: function() 
                        {

                        }
                    });
            }, 1000);
            e.preventDefault();	//STOP default action
    });
    
    $( "#payment_method" ).change(function() {
            if($(this).val() == ""){
                alert("Vui lòng chọn Phương thức thanh toán");return false;
            }
            $('#note').removeAttr('readonly');
            if($("#payment_method").val() != 3){
                $("#payment_method_form").html('');
                
                return false;
            }
            $('button[type="submit"]').removeAttr('disabled');
            $.ajax(
            {
                    url : '/auth/booking/paymentMethod?id='+$(this).val()+'&cid='+$("#customerID").val(),
                    success:function(data)
                    {
                       $("#payment_method_form").html(data);
                    },
                    error: function() 
                    {
                        alert("Lỗi! Liên hệ Admin!");return false;
                    }
            });
   });
   
    var fone_add = 2;
    $("button.btn-success").on('click',function (e) {
            $("#foneadd").append('<div id="fone_addon'+fone_add+'" class="fonediv" adata="'+fone_add+'"><input type="text" name="booking_place_code[]" value="" id="'+fone_add+'" class="num_cls"> <button name="" type="button" adata="'+fone_add+'" class="btn btn-danger" onclick="javascript:$(this).parent().remove();">Xóa</button><span id="simple-msg-phone'+fone_add+'"></span></div>');
            fone_add = fone_add+1;
    });
    
    function FormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
        }
    };
    function UnFormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/,|VNĐ|\s/g, "");
        }
    };
});
</script>
